package com.example.productdataserviceprovider.service.utils;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HttpUtils {
    public static String doGet(String url)throws Exception{
        CloseableHttpClient httpclient = HttpClients.createDefault();
        if(url.toLowerCase().startsWith("https")){
            httpclient = HttpSSLClient.getHttpClient();
        }

        //        // 创建参数队列
//        List<NameValuePair> params = new ArrayList<NameValuePair>();
//        params.add(new BasicNameValuePair("username", "admin"));
//        params.add(new BasicNameValuePair("password", "123456"));
        String result=null;
        try {
            // 创建httpget.
            HttpGet httpGet = new HttpGet(url);
            httpGet.setHeader("Connection", "keep-alive");
            httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            httpGet.setHeader("Accept-Encoding", "gzip, deflate, br");
            httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7");
            httpGet.setHeader("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36");
            httpGet.setHeader("Cookie","BAIDUID=5548F404790F0EF9089B625E84331679:FG=1; BIDUPSID=5548F404790F0EF9089B625E84331679; PSTM=1546951184; MSA_WH=375_812; H_WISE_SIDS=142066_143857_143389_142761_142206_143879_141876_141746_143162_143422_142514_139177_142919_142779_131247_141261_138165_138883_141942_127969_140065_143368_140593_134046_143059_141808_137911_140350_138425_141008_143471_143923_143276_141930_131423_144005_107313_138595_139882_143478_143045_142912_143548_142278_110085; BD_UPN=12314453; BDUSS=lnV2cxLTdKd1ZTaTlEMXl1MG5VN29-bXRscWJONUZibDU1a1Y3V1EyVXRSVmRmRVFBQUFBJCQAAAAAAAAAAAEAAABKHFYAd2p4XzU4OTMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC24L18tuC9fQn; BDUSS_BFESS=lnV2cxLTdKd1ZTaTlEMXl1MG5VN29-bXRscWJONUZibDU1a1Y3V1EyVXRSVmRmRVFBQUFBJCQAAAAAAAAAAAEAAABKHFYAd2p4XzU4OTMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC24L18tuC9fQn; delPer=0; BD_CK_SAM=1; PSINO=2; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BD_HOME=1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; H_PS_645EC=5396V0XI%2BWalwev4gXOY%2FeWucFkYDHQCH%2BoUt5bw9y3c6QB6l3UiHF1IAPwk%2FUPlYf2y; H_PS_PSSID=1468_32539_31253_32046_32116_32091_32482; BDSVRTM=0");
            httpGet.setHeader("Host", "www.baidu.com");
            httpGet.setHeader("Sec-Fetch-Dest", "document");
            httpGet.setHeader("Sec-Fetch-Mode", "navigate");
            httpGet.setHeader("Sec-Fetch-Site", "none");
            httpGet.setHeader("Sec-Fetch-User", "?1");
            httpGet.setHeader("Upgrade-Insecure-Requests", "1");
            RequestConfig config=RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
            httpGet.setConfig(config);
            CloseableHttpResponse httpResponse = httpclient.execute(httpGet);
            try {
                HttpEntity entity = httpResponse.getEntity();

//                System.out.println("Entity====="+entity.getContentType());
                String charset="UTF-8";
                result=EntityUtils.toString(entity,charset);
                if(result.contains("gbk")||result.contains("gb2312")||result.contains("GBK")||result.contains("GB2312"))
                {
                    charset="GBK";
                    httpResponse = httpclient.execute(httpGet);
                    entity = httpResponse.getEntity();
                    result = EntityUtils.toString(entity, charset);
                }
                return result;
            } finally {
                httpResponse.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    public static String removeHtmlTag(String htmlStr){
        String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
        String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
        String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式

        Pattern p_script= Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
        Matcher m_script=p_script.matcher(htmlStr);
        htmlStr=m_script.replaceAll(""); //过滤script标签

        Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
        Matcher m_style=p_style.matcher(htmlStr);
        htmlStr=m_style.replaceAll(""); //过滤style标签

        Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
        Matcher m_html=p_html.matcher(htmlStr);
        htmlStr=m_html.replaceAll(""); //过滤html标签

        return htmlStr.trim(); //返回文本字符串
    }

}
